/**
 * Created by Administrator on 2018/3/23 0023.
 */
const models = require('../models')

const { Catalog } = models
function showByRootTree(catalogs) {
  const res = []
  for (let i = 0; i < catalogs.length; i++) {
    if (catalogs[i].deep === 0) {
      res.push(catalogs[i])
      continue
    }
    const index = res.findIndex((item) => item.name === catalogs[i].name)
    if (index === -1) {
      res.push(catalogs[i])
    }
    const tmp = []
    for (let j = i + 1; j < catalogs.length; j++) {
      if (catalogs[j].deep > catalogs[i].deep && catalogs[j].calPath === `${catalogs[i].calPath},${catalogs[i].name}`) {
        tmp.push(catalogs[j])
      }
    }
    if (tmp.length > 0) {
      const newIndex = res.findIndex((item) => item.name === catalogs[i].name)
      Array.prototype.splice.apply(res, [newIndex + 1, 0, ...tmp])
    }
  }
  return res
}
exports.getCatalogByShopName = function (shopName, path, callback) {
  Catalog.find({ shopName, calPath: path }, callback)
}
exports.getCatalogMenu = function (callback) {
  const catalog = Catalog.find({ shopName: 'mShop' })
  catalog.sort({ calPath: 1, sort: 1 })
  catalog.exec((err, catalogs) => {
    if (err) return callback(err, [])
    callback(err, showByRootTree(catalogs))
  })
}

// 获取前端页面列表
exports.getFrontCatalog = function (callback) {
  const catalog = Catalog.find({ shopName: 'mShop', calPath: ',root,front' })
  catalog.sort({ calPath: 1, sort: 1 })
  catalog.exec(callback)
}
// 返回当前路径的子目录
exports.getChildrenByName = function (name, callback) {
  Catalog.findOne({ name }, (err, catalog) => {
    if (err) return callback(err)
    const catalogs = Catalog.find({ calPath: `${catalog.calPath},${name}` })
    catalogs.sort({ sort: 1 })
    catalogs.exec(callback)
  })
}
// 返回当前路径所有子目录
exports.getChildrenByNameAll = function (name, callback) {
  Catalog.findOne({ name }, (err, catalog) => {
    if (err) return callback(err)
    const catalogs = Catalog.find({ calPath: new RegExp(`^${catalog.calPath},${name}`) })
    catalogs.sort({ sort: 1 })
    catalogs.exec((err, list) => {
      if (err) return callback(err, [])
      callback(err, showByRootTree(list))
    })
  })
}
function coverDeep(arr, deep) {
  return arr.filter((item) => item.deep === deep)
}
function arr2Child(arr, child, num = 2) {
  let deepArr = arr
  let deep = num || 2
  if (child.length === 0) return
  if (num === 2) {
    deepArr = coverDeep(arr, num)
  } else {
    deepArr = child
  }
  deep++
  deepArr.forEach((item) => {
    const newArr = coverDeep(arr, deep)
    if (newArr.length === 0) return
    item.children = []
    newArr.forEach((subItem) => {
      if (`${item.calPath},${item.name}` === subItem.calPath) {
        item.children.push(subItem)
      }
    })
    if (item.children.length === 0) return
    arr2Child(arr, item.children, deep)
  })
  return deepArr
}
exports.getChildModeByName = function (name, callback) {
  Catalog.findOne({ name }, (err, catalog) => {
    if (err) return callback(err)
    const catalogs = Catalog.find({ calPath: new RegExp(`^${catalog.calPath},${name}`) })
    catalogs.sort({ sort: 1 })
    catalogs.exec((err, cats) => {
      if (err) return callback(err, [])
      // console.log(cats, '-----catalogs---')
      const arr = showByRootTree(cats)
      const newCats = arr.map((item) => item.toObject())
      // console.log(newCats, '--------arr-------')
      const childs = arr2Child(newCats, newCats)
      // console.log(childs, '--------childs-------')
      callback(err, childs, arr)
    })
  })
}
exports.findByName = function (name, callback) {
  Catalog.findOne({ name }, callback)
}

exports.findByTpl = function (useTpl, callback) {
  const catalog = Catalog.find({ useTpl, shopName: 'mShop' })
  catalog.sort({ calPath: 1 })
  catalog.exec(callback)
}

exports.removeById = function (id, callback) {
  Catalog.findByIdAndRemove(id, callback)
}
exports.findAndUpdate = function (id, obj, callback) {
  Catalog.findByIdAndUpdate(id, obj, callback)
}
exports.create = function (obj, callback) {
  const catalog = new Catalog(obj)
  catalog.save(callback)
}
